package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //    @Autowired
//    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Transactional
    public void saveSetmealWithSetmealDish(SetmealDTO setmealDTO) {
        //1、保存套餐基本信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.save(setmeal);

        //2、保存套餐对应的菜品信息
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishMapper.saveBatch(setmealDishes);
    }

    /**
     * 套餐的分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {


        //方法1、多表联查
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealPage = setmealMapper.page(setmealPageQueryDTO);
        List<SetmealVO> setmealVOList = setmealPage.getResult();
        PageResult pageResult = new PageResult(setmealPage.getTotal(), setmealVOList);


//方法二，不用多表联查
//        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
//        Page<SetmealVO> setmealPage = setmealMapper.page(setmealPageQueryDTO);
//        List<SetmealVO> setmealVOList = setmealPage.getResult();
//        for (SetmealVO setmealVO : setmealVOList) {
//            Long categoryId = setmealVO.getCategoryId();
//            String categoryName = categoryMapper.queryNameByCategoryId(categoryId);
//            setmealVO.setCategoryName(categoryName);
//        }
//        PageResult pageResult = new PageResult(setmealPage.getTotal(), setmealVOList);
        return pageResult;
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    public SetmealVO findSetmealById(Long id) {
        //根据id查询套餐
        SetmealVO setmealVO = setmealMapper.queryById(id);

        //根据id查询套餐下关联的菜品
        List<SetmealDish> setmealDishList = setmealDishMapper.queryDishsBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishList);

        return setmealVO;
    }

    /**
     * 根据id更新套餐
     *
     * @param setmealDTO
     */
    @Transactional
    public void updateSetmealById(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //更新套餐基本信息
        setmealMapper.update(setmeal);

        //更新套餐菜品关联表信息
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteBatchBySetmealId(setmealId);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        if (setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
            setmealDishMapper.saveBatch(setmealDishes);
        }
    }

    /**
     * 套餐的启售或停售
     *
     * @param status
     * @param id
     */
    @Transactional
    public void startOrStop(Integer status, Long id) {

        if (status == StatusConstant.ENABLE) {
            //SELECT * FROM setmeal_dish as sd LEFT JOIN dish as d on sd.dish_id = d.id
            // where sd.setmeal_id = ?;

            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if (dishList != null && dishList.size() > 0) {
                for (Dish dish : dishList) {
                    if (dish.getStatus() == StatusConstant.DISABLE) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }

        }


        Setmeal setmeal = Setmeal.builder().
                id(id).
                status(Integer.valueOf(status)).
                build();

        setmealMapper.update(setmeal);

    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        List<Setmeal> setmealList = setmealMapper.getBatchByIds(ids);
        for (Setmeal setmeal : setmealList) {
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        for (Long setmealId : ids) {
            //删除套餐表信息
            setmealMapper.deleteById(setmealId);
            //删除套餐菜品表信息
            setmealDishMapper.deleteBySetmealId(setmealId);
        }

    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
